/*
 * This implementation of FIFO Queue that will be used for 
 * the received message queue.
 * uturan 05/05/2011

*/
#include <stdlib.h>
#include <stdio.h>
#include "Queue.h"


QUEUE_TYPE* initQueue(U32 n)
{
	QUEUE_TYPE *q;

    if ((q = malloc(sizeof(QUEUE_TYPE)))== NULL)
        return NULL;

    if ((q->nodes = malloc((n ) * sizeof(void*))) == NULL) {
        free(q);
        return NULL;
    }

    q->currentSize = 0;
    q->availableSize = (n);

    return q;
}

void freeQueue(QUEUE_TYPE *q)
{
    free(q->nodes);
    free(q);
}

STATUS insertNodeQueue(QUEUE_TYPE *q, void *node)
{
    //check whether queue is available to insert new item
    if ( q == NULL || (q->currentSize >= q->availableSize)) {
    	printf("QUEUE FULL!! \n");
    	return ERROR;
    }

    /* insert an item into Queue*/
    q->nodes[q->currentSize] = node;
    q->currentSize++;

    return OK;
}

void* popQueue(QUEUE_TYPE *q)
{
    U32 i;
	void* topNode;

    if ( q == NULL || q->currentSize == 0 )
        return NULL;

    topNode = q->nodes[0];

    q->currentSize--;
    
    for( i = 0; i < q->currentSize; i++) 
         q->nodes[i] = q->nodes[i + 1];
    
    return topNode;
}
